<?php

Class Sales_order extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
	}

	function index()
	{
		$this->tampil();
	}

	function tampil()
	{
       $ilc['tanggal'] = date("d-m-Y");
       
       if($this->uri->segment(4)==true){
           $ses_saleid = $this->uri->segment(4);
           $res_saleid = $this->uri->segment(4);
           $saleid = $this->uri->segment(4);
           $res    = $this->mso->get_sale_detail($saleid)->row();
           $row    = $this->mso->get_sale($saleid)->row();
           $tampil = $this->mso->get_sale_detail($saleid)->result();
           $submit = "ubah";
       }else{
           $ses_saleid = $this->session->userdata('saleid');
           //echo $ses_saleid;
           $res_saleid = "";
           $res = "";
           $saleid = $this->session->userdata('saleid');
           $row    = $this->mso->get_sale($saleid)->row();
           $tampil = $this->mso->get_sale_detail($saleid)->result();
           $submit = "simpan";
       }
       
       $ilc['row']        = $row;
       $ilc['res']        = $res;
       $ilc['tampil']     = $tampil;
       $ilc['ses_saleid'] = $ses_saleid;
       $ilc['res_saleid'] = $res_saleid;
       $ilc['saleid']     = $saleid;
       $ilc['submit']     = $submit;
        
	   $this->load->view('transaksi/vsales_order',$ilc);
	}	

	function simpan()
	{
        if($this->input->post('simpan')){
            
            if($this->input->post('saleid')){
                $saleid  = $this->input->post('saleid');
            }else{
                $saleid  = $this->mso->saleid();            
            }
            
    		$kepada   = $this->input->post('pelanggan1');
    		$untuk    = $this->input->post('pelanggan2');
            $sales    = $this->input->post('sales');
            $tender   = $this->input->post('tender');
            $jenis    = $this->input->post('tipetrn');
            $tanggal  = $this->input->post('tanggal');
            
            $prokode     = $this->input->post('prokode');
            $phg         = $this->input->post('phg');
            $price       = $this->input->post('harga');
            $qty         = $this->input->post('qty');
            $diskon      = $this->input->post('disc');
            $total       = $this->input->post('total');
            
            $stat = 1;
            $this->db->trans_start();
            $cek  = $this->mso->cek($saleid);
            
            if($cek == '0'){
                $stat *= $this->mso->sale($saleid,$kepada,$untuk,$sales,$tender,$jenis,$tanggal);
            }
            $stat *= $this->mso->sale_detail($saleid,$prokode,$phg,$price,$qty,$diskon,$total,$stat);
            //$this->mso->sale_detail($noso,$prokode,$nama,$satuan,$qty,$harga,$diskon,$total,$phg);
            if($stat){ 
                $this->db->trans_commit();
            }else{
                $this->db->trans_rollback();
                $this->session->set_flashdata('alert','Transaksi Gagal!');
            }
            $this->session->set_userdata('saleid',$saleid);
        }
        
        if($this->input->post('ubah')){
            $saleid      = $this->input->post('saleid');
            $prokode     = $this->input->post('prokode');
            $phg         = $this->input->post('phg');
            $price       = $this->input->post('harga');
            $qty         = $this->input->post('qty');
            $diskon      = $this->input->post('disc');
            $total       = $this->input->post('total');
            $no          = $this->input->post('no');

            $stat = 1;
            $this->db->trans_start();
            $stat *= $this->mso->update_sale_detail($no,$saleid,$prokode,$phg,$price,$qty,$diskon,$total,$stat);
            if($stat){ 
                $this->db->trans_commit();
            }else{
                $this->db->trans_rollback();
                $this->session->set_flashdata('alert','Transaksi Gagal!');
            }
            $this->session->set_userdata('saleid',$saleid);
        }
        
        if($this->input->post('clear')){
            $this->session->unset_userdata('saleid');
        }
        
		redirect('transaksi/sales_order');
	}

	function hapus()
	{
		$no = $this->uri->segment(4);

		$this->mso->hapus($no);
		redirect('transaksi/sales_order');
	}
    
    function pilih_harga()
    {
        $phg = $this->input->get('phg');
        $prokode = $this->input->get('prokode');
        
        $harga  = $this->mso->pilih_harga($phg,$prokode);
        
        echo $harga;
    }
}